
*------------------------------------------------------------------------------*
* PM negotiates with portfolio minister(s) or cabinet (weighted position)
*
* Additional specs negotiation with cabinet partner(s)
*	1. Fallback 1: Prime minister position if single party cabinet
*	2. Fallback 2: 1st chamber median for expert governments
*
* Additional specs negotiation with minister(s)
*	1. Unweighted mean if two or more ministers
*	2. Fallback 1: Prime minister position if no minister(s) present
*	3. Fallback 2: 1st chamber median for expert governments
*
* Version:	2016-06-08
* Changes:	2016-??-??: First release
*------------------------------------------------------------------------------*


* Parse minister
local minister "$minister"
*di "`minister'"
tokenize "`minister'", parse(",")
local minister1 `1'
local minister2 `3'
local minister3 `5'
local minister4 `7'
local minister5 `9'
*di "`minister1'"
*di "`minister2'"
*di "`minister3'"
*di "`minister4'"
*di "`minister5'"


*-----------------------------------------*
* PM negotiates with cabinet parties

if "`minister1'" == "cabinet" {

	* Strength of PMs party
	gen zsseatspm = p303 if p103 == 1 & $ideology != . & p201 == 1	
	by g101 g105, sort: egen zsseats_pm =sum(zsseatspm)	
	* Position of PM
	gen zspospm = $ideology if p103 == 1 & $ideology != . & p201 == 1	
	by g101 g105, sort: egen zspos_pm =sum(zspospm)		

	
	* Strength of cabinet partner(s)
	gen zsseatsmin1 = p303 if p103 == 1 & $ideology != . & p201 != 1
	* Weighted position(s): PM <-> cabinet partner
	gen zsweight1 = zsseatsmin1 / (zsseats_pm + zsseatsmin1)
	gen zsposmin1 = zsweight1 * $ideology 		// weight*pos of partner in "two party coalition"
	gen zspospmw1 = (1 - zsweight1) * zspos_pm	// weight*pos of PM in "two party coalition"
	gen zspos1 = zspospmw1 + zsposmin1
	by g101 g105, sort: egen pos1 = mean(zspos1)
	

	* Fill in government position
	gen GOV_POS = .
	replace GOV_POS = pos1
	replace GOV_POS = zspos_pm if GOV_POS == .	// Fallback 1: PMs position if no minister present	


}
*-----------------------------------------*
* PM negotiates with portfolio minister(s)

else {
	
	* Strength of PMs party
	gen origin = .
	gen zsseatspm = p303 if p103 == 1 & $ideology != . & p201 == 1	
	by g101 g105, sort: egen zsseats_pm =sum(zsseatspm)	
	* Position of PM
	gen zspospm = $ideology if p103 == 1 & $ideology != . & p201 == 1	
	by g101 g105, sort: egen zspos_pm =sum(zspospm)		
	
	
	* Strength of 1st minister(s)
	gen zsseatsmin1 = p303 if p103 == 1 & $ideology != . & `minister1' > 0
	replace origin = 1 if zsseatsmin1 != . & origin == .
	* Weighted position(s): PM <-> 1st minister(s)
	gen zsweight1 = zsseatsmin1 / (zsseats_pm + zsseatsmin1) if origin == 1
	gen zsposmin1 = zsweight1 * $ideology 		// weight*pos of minister in "two party coalition"
	gen zspospmw1 = (1 - zsweight1) * zspos_pm	// weight*pos of PM in "two party coalition"
	gen zspos1 = zspospmw1 + zsposmin1
	by g101 g105, sort: egen pos1 = mean(zspos1)
	

	* Strength of 2nd minister(s)
	capture: gen zsseatsmin2 = p303 if p103 == 1 & $ideology != . & `minister2' > 0
	capture: replace origin = 2 if zsseatsmin2 != . & origin == .
	* Weighted position(s): PM <-> 2nd minister(s)
	capture: gen zsweight2 = zsseatsmin2 / (zsseats_pm + zsseatsmin2) if origin == 2
	capture: gen zsposmin2 = zsweight2 * $ideology 		// weight*pos of minister in "two party coalition"
	capture: gen zspospmw2 = (1 - zsweight2) * zspos_pm	// weight*pos of PM in "two party coalition"
	capture: gen zspos2 = zspospmw2 + zsposmin2
	capture: by g101 g105, sort: egen pos2 = mean(zspos2)
	

	* Strength of 3rd minister(s)
	capture: gen zsseatsmin3 = p303 if p103 == 1 & $ideology != . & `minister3' > 0
	capture: replace origin = 3 if zsseatsmin3 != . & origin == .
	* Weighted position(s): PM <-> 3rd minister(s)
	capture: gen zsweight3 = zsseatsmin3 / (zsseats_pm + zsseatsmin3) if origin == 3
	capture: gen zsposmin3 = zsweight3 * $ideology 		// weight*pos of minister in "two party coalition"
	capture: gen zspospmw3 = (1 - zsweight3) * zspos_pm	// weight*pos of PM in "two party coalition"
	capture: gen zspos3 = zspospmw3 + zsposmin3
	capture: by g101 g105, sort: egen pos3 = mean(zspos3)
	
	
	* Strength of 4th minister(s)
	capture: gen zsseatsmin4 = p303 if p103 == 1 & $ideology != . & `minister4' > 0
	capture: replace origin = 4 if zsseatsmin4 != . & origin == .
	* Weighted position(s): PM <-> 4th minister(s)
	capture: gen zsweight4 = zsseatsmin4 / (zsseats_pm + zsseatsmin4) if origin == 4
	capture: gen zsposmin4 = zsweight4 * $ideology 		// weight*pos of minister in "two party coalition"
	capture: gen zspospmw4 = (1 - zsweight4) * zspos_pm	// weight*pos of PM in "two party coalition"
	capture: gen zspos4 = zspospmw4 + zsposmin4
	capture: by g101 g105, sort: egen pos4 = mean(zspos4)
	
	
	* Strength of 5th minister(s)
	capture: gen zsseatsmin5 = p303 if p103 == 1 & $ideology != . & `minister5' > 0
	capture: replace origin = 5 if zsseatsmin5 != . & origin == .
	* Weighted position(s): PM <-> 5th minister(s)
	capture: gen zsweight5 = zsseatsmin5 / (zsseats_pm + zsseatsmin5) if origin == 5
	capture: gen zsposmin5 = zsweight5 * $ideology 		// weight*pos of minister in "two party coalition"
	capture: gen zspospmw5 = (1 - zsweight5) * zspos_pm	// weight*pos of PM in "two party coalition"
	capture: gen zspos5 = zspospmw5 + zsposmin5
	capture: by g101 g105, sort: egen pos5 = mean(zspos5)

	
	* Fill in government position
	by g101 g105, sort: egen origin2 = min(origin)
	gen GOV_POS = .
	replace GOV_POS = pos1 if origin2 == 1
	capture: replace GOV_POS = pos2 if GOV_POS == . & origin2 == 2
	capture: replace GOV_POS = pos3 if GOV_POS == . & origin2 == 3
	capture: replace GOV_POS = pos4 if GOV_POS == . & origin2 == 4
	capture: replace GOV_POS = pos5 if GOV_POS == . & origin2 == 5
	capture: replace GOV_POS = zspos_pm if GOV_POS == . & origin2 == .	// Fallback 1: PMs position if no minister present


}
save zs, replace	


* Fill in GOV_POS with fallback 2
replace GOV_POS = median1st if zsseats_pm == 0 & GOV_POS == 0


* Cleansing
replace GOV_POS = . if g106 == 9

lab var GOV_POS "Position of government"

sort g101 g105 p101

capture: macro drop minister
capture: drop zs*
capture: drop origin*
capture: drop pos* 
capture: erase zs.dta
capture: erase zs_median.dta
